package com.tungse.study.activiti;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;

public class Demo2 {


    /**
     * 启动任务示例
     */
    @Test
    public void testStartProcessInstance() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();

        //启动流程实例
        ProcessInstance leaveProcess = runtimeService.startProcessInstanceByKey ("leaveProcess");
        //流程实例ID
        System.out.println(leaveProcess.getId());
        System.out.println(leaveProcess.getProcessDefinitionId());
    }
    /**
     * 查询待办任务
     */
    @Test
    public void testQueryTask() {
        //基于当前用户查询其待办任务
        String currentUser = "张三";
        String processInstanceId = "2501";
        //1创建流程引擎对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2获取流程实例对象
        TaskService taskService = processEngine.getTaskService();
        //3创建查询对象
        TaskQuery taskQuery = taskService.createTaskQuery()
                //指定流程负责人
                .taskAssignee(currentUser)
                //指定流程实例ID
                .processInstanceId(processInstanceId)
                //查询未完成的任务
                .active();
        //执行查询
        taskQuery.list().forEach(task -> {
           System.out.println("任务ID：" + task.getId());
        });
        //4打印任务
    }

}
